import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import qs from 'qs'
import global from './global/global.vue'
import store from './store'
import cookies from 'vue-cookies'
import echarts from 'echarts'
import "./assets/directive.js";
import {formatTimeToStr} from "./assets/format.js"
import Print from "vue-print-nb"
Vue.use(Print);// 打印
import XLSX from 'xlsx' // excel导入
import JsonExcel from 'vue-json-excel' // excel导出
Vue.component('downloadExcel', JsonExcel)
Vue.use(XLSX)
Vue.config.productionTip = false
Vue.prototype.qs = qs
Vue.prototype.global = global
Vue.prototype.cookies = cookies
Vue.prototype.$echarts = echarts;//绘制图表
// 开启axios请求携带cookie
axios.defaults.withCredentials = true

// 路由守卫，阻止没登陆者进入主页，并跳转到登录页面
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.withoutLogin)) {
        next()
    } else {
		if (localStorage.getItem("isLogin") || store.state.isLogin) {
            next()
        } else {
            next({ path: '/login' })
        }
    }
})
// 请求拦截器
axios.interceptors.request.use(config => {
    if (store.state.user) {
        cookies.set('userId', store.state.user.userId)
    }
    return config;
}, error => {
    // Do something with request error
    return Promise.reject(error);
});
// 全局日期格式化过滤器
Vue.filter("format_date", function (value){
    return formatTimeToStr(value, "yyyy-MM-dd")
})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
